<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"
                xmlns:atom="http://www.w3.org/2005/Atom"
                xmlns:xhtml="http://www.w3.org/1999/xhtml"
                xmlns:planet="http://planet.intertwingly.net/"
                xmlns="http://www.w3.org/1999/xhtml"
                exclude-result-prefixes="atom planet xhtml">
 
  <xsl:output method="xml" omit-xml-declaration="yes"/>

  <xsl:template match="atom:feed">
    <xsl:text disable-output-escaping="yes">&lt;!DOCTYPE html&gt;</xsl:text>
    <html xmlns="http://www.w3.org/1999/xhtml">

      <!-- head -->
      <head>
        <title><xsl:value-of select="atom:title"/></title>
        <meta name="generator" content="{atom:generator}" />
        <link rel="stylesheet" href="zero.css" type="text/css" />
        <link rel="alternate" href="atom.xml" title="{atom:title}" type="application/atom+xml" />
        <link rel="shortcut icon" href="/favicon.ico" />
      </head>

      <body>
        <div class="header">
        <h1><xsl:value-of select="atom:title"/></h1>
        </div>

        <div class="content">
          <xsl:apply-templates select="atom:entry"/>
        </div>


        <div class="sidebar">
          <div>
          Feed: <a href="atom.xml"><img src="images/feed-icon-10x10.png" alt="(feed)" border="0" /> Atom 1.0</a><br/>
          Subscription list: <a href="foafroll.xml">FOAF</a>, <a href="opml.xml">OPML</a>
          <br/><br/>
          Last update: <xsl:value-of select="atom:updated/@planet:format"/><br/>
          <em>All times are UTC.</em>
          </div>

          <div>
            <h2>Subscriptions</h2>
            <ul>
            <xsl:for-each select="planet:source">
              <xsl:sort select="planet:name"/>
              <xsl:variable name="id" select="atom:id"/>
              <xsl:variable name="posts"
                select="/atom:feed/atom:entry[atom:source/atom:id = $id]"/>
              <xsl:text>&#10;</xsl:text>
              <li>
                <a title="subscribe">
                  <xsl:choose>
                    <xsl:when test="planet:http_location">
                      <xsl:attribute name="href">
                        <xsl:value-of select="planet:http_location"/>
                      </xsl:attribute>
                    </xsl:when>
                    <xsl:when test="atom:link[@rel='self']/@href">
                      <xsl:attribute name="href">
                        <xsl:value-of select="atom:link[@rel='self']/@href"/>
                      </xsl:attribute>
                    </xsl:when>
                  </xsl:choose>
                  <xsl:choose>
                    <xsl:when test="planet:message">
                      <xsl:attribute name="class">
                        <xsl:if test="$posts">active message</xsl:if>
                        <xsl:if test="not($posts)">message</xsl:if>
                      </xsl:attribute>
                      <xsl:attribute name="title">
                        <xsl:value-of select="planet:message"/>
                      </xsl:attribute>
                    </xsl:when>
                    <xsl:when test="atom:title">
                      <xsl:attribute name="title">
                        <xsl:value-of select="atom:title"/>
                      </xsl:attribute>
                      <xsl:if test="$posts">
                        <xsl:attribute name="class">active</xsl:attribute>
                      </xsl:if>
                    </xsl:when>
                  </xsl:choose>
                  <img src="images/feed-icon-10x10.png" alt="(feed)" border="0" />
                  <xsl:text> </xsl:text>
                  <xsl:value-of select="planet:name"/>
                </a>
              </li>
            </xsl:for-each>
            </ul>
          </div>

          <div>
             If you would like additional feeds added to the subscriptions, please <a href="mailto:brandons@us.ibm.com">contact me</a>.
          </div>

        </div>

        <div id="footer">
          <ul id="bn">
            <li><a href="http://stlzro1.torolab.ibm.com/wiki/bin/view/Main/WebHome">Home</a></li>
            <li><a href="http://stlzro1.torolab.ibm.com/wiki/bin/view/Community/WebHome">Community</a></li>
            <li><a href="http://stlzro1.torolab.ibm.com/wiki/bin/view/Documentation/WebHome">Documentation</a></li>
            <li><a href="http://stlzro1.torolab.ibm.com/wiki/bin/view/Download/WebHome">Download</a></li>
            <li><a href="http://stlzro1.torolab.ibm.com/wiki/bin/view/Main/About">About</a></li>
          </ul>
          <p>Maintained by <a href="mailto:brandons@us.ibm.com">Brandon Smith</a>, powered by <a href="http://www.intertwingly.net/code/venus/">Planet Venus</a></p>
        </div>

      </body>
    </html>
  </xsl:template>
 
  <xsl:template match="atom:entry">
    <!-- date header -->
    <xsl:variable name="date" select="substring(atom:updated,1,10)"/>
    <xsl:if test="not(preceding-sibling::atom:entry[substring(atom:updated,1,10) = $date])">
      <h2>
        <xsl:value-of select="substring-before(atom:updated/@planet:format,', ')"/>
        <xsl:text>, </xsl:text>
        <xsl:value-of select="substring-before(substring-after(atom:updated/@planet:format,', '), ' ')"/>
      </h2>
    </xsl:if>

    <xsl:variable name="current_title" select="atom:source/atom:title" />
    <xsl:if test="not(preceding-sibling::atom:entry[atom:source/atom:title = $current_title])">
      <h3>
        <a href="{atom:link[@rel='alternate']/@href}"><xsl:value-of select="atom:source/atom:title"/></a>
      </h3>
    </xsl:if>

    <!-- entry title -->

    <h4>
      <xsl:if test="string-length(atom:title) > 0">
        <a href="{atom:link[@rel='alternate']/@href}">
          <xsl:if test="atom:title/@xml:lang != @xml:lang">
            <xsl:attribute name="xml:lang" select="{atom:title/@xml:lang}"/>
          </xsl:if>
          <xsl:value-of select="atom:title"/>
        </a>
      </xsl:if>
    </h4>

    <h5>
      <xsl:value-of select="atom:updated/@planet:format"/><xsl:text> | </xsl:text><xsl:value-of select="atom:author/atom:name"/>
    </h5>

    <!-- entry content -->
    <div class="entry">
      <xsl:choose>
        <xsl:when test="atom:content">
          <xsl:apply-templates select="atom:content"/>
        </xsl:when>
        <xsl:otherwise>
          <xsl:apply-templates select="atom:summary"/>
        </xsl:otherwise>
      </xsl:choose>
    </div>

  </xsl:template>

  <!-- xhtml content -->
  <xsl:template match="atom:content/xhtml:div | atom:summary/xhtml:div">
    <xsl:copy>
      <xsl:if test="../@xml:lang and not(../@xml:lang = ../../@xml:lang)">
        <xsl:attribute name="xml:lang">
          <xsl:value-of select="../@xml:lang"/>
        </xsl:attribute>
      </xsl:if>
      <xsl:attribute name="class">content</xsl:attribute>
      <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
  </xsl:template>

  <!-- plain text content -->
  <xsl:template match="atom:content/text() | atom:summary/text()">
    <div class="content" xmlns="http://www.w3.org/1999/xhtml">
      <xsl:if test="../@xml:lang and not(../@xml:lang = ../../@xml:lang)">
        <xsl:attribute name="xml:lang">
          <xsl:value-of select="../@xml:lang"/>
        </xsl:attribute>
      </xsl:if>
      <xsl:copy-of select="."/>
    </div>
  </xsl:template>

  <!-- Remove stray atom elements -->
  <xsl:template match="atom:*">
    <xsl:apply-templates/>
  </xsl:template>

  <!-- Feedburner detritus -->
  <xsl:template match="xhtml:div[@class='feedflare']"/>

  <!-- Strip site meter -->
  <xsl:template match="xhtml:div[comment()[. = ' Site Meter ']]"/>

  <!-- pass through everything else -->
  <xsl:template match="@*|node()">
    <xsl:copy>
      <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
  </xsl:template>

</xsl:stylesheet>
